//
//  DZTabBar.swift
//  Swift微博
//
//  Created by 董筱泽 on 16/10/22.
//  Copyright © 2016年 董筱泽. All rights reserved.
//

import UIKit

class DZTabBar: UITabBar {
    var closure: (()->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc private func btnClick(){
    
        closure?()
    }
    
    func setupUI(){
        
        addSubview(composeButtom)
    }
    // MARK: - 设置视图
    override func layoutSubviews() {
        super.layoutSubviews()
        let W = frame.width * 0.2
        var index: CGFloat = 0
        for value in subviews{
            // 判断是否是我们想要的UITabBarButton
            //UITableViewCell.self  == [UITableViewCell class]
            // 判断 class 一致
            if value.isKind(of: NSClassFromString("UITabBarButton")!) {
                // 设置 x 和 w
                value.frame.size.width = W
                value.frame.origin.x = index * W
                index += 1
                if index == 2 {
                    index += 1
                }
            }
            
        }
        composeButtom.center.x = frame.width * 0.5
        composeButtom.center.y = frame.height * 0.5
    }
    // MARK: - 懒加载控件
    // 撰写按钮
    lazy var composeButtom: UIButton = {
        let buttom = UIButton(setImageName: "tabbar_compose_icon_add", backGroundName: "tabbar_compose_button", target: self, action: #selector(btnClick))
        
        return buttom
    }()
}
